package cn.edu.pet.servlet.admin;

import cn.edu.pet.entity.AdoptRecord;
import cn.edu.pet.entity.Pets;
import cn.edu.pet.repo.AdoptRecordRepo;
import cn.edu.pet.repo.PetRepo;
import cn.edu.pet.util.PaginationUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/admin/adopt/list")
public class AdoptRecordListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        String page1 = req.getParameter("page");
        Integer type = req.getParameter("name").equals("") ? null : Integer.valueOf(req.getParameter("name"));
        List<AdoptRecord> list = AdoptRecordRepo.getInstance().selectList(type);
        PaginationUtil<AdoptRecord> page = new PaginationUtil<>();
        PrintWriter writer = resp.getWriter();
        String paginate = page.paginate(list, Integer.parseInt(page1));
        writer.println(paginate);
    }
}
